package com.sejon.firebase;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * Description:
 *
 * @author <a href="mailto:gsj@janesi.com">gsj</a>
 * @Date Create on 2018/12/14 12:00
 * @since JDK1.8 Copyright JANESI All Rights Reserved.
 */
public class MessageDemo {
    private static final String PROJECT_ID = "uang-cash-5b9cb";
    private static final String BASE_URL = "https://fcm.googleapis.com";
    private static final String FCM_SEND_ENDPOINT = "/v1/projects/" + PROJECT_ID + "/messages:send";

    private static final String MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
    private static final String[] SCOPES = { MESSAGING_SCOPE };

    private static final String TITLE = "FCM Notification";
    private static final String BODY = "Notification from FCM";
    public static final String MESSAGE_KEY = "message";
    private static final String TOKEN = "cXKfYJ1nvAA:APA91bHaMz7LcUiUWKIp_13ZpZr9AQXRXJL-9na5ksZR1dqZH-dUMf_Dv-L9UknGfY4NdJ7k4SHZcr9ULqXhgaLXo7TrwkCiFTlCI_sgg8XaQ9io9ahSZduDlGqzBbgf4NTs6Z1nki5h";
    private static final String TOKEN2 = "ckoBbd2f05M:APA91bG_qs_wVSPrGqQ6kHBG_QcB5uGKgxZjFF4PwR2Mfx1X5SQNqI0zLJJ0v3R2mnHLgKNS2PiDUbXFyHYnTJUsX2O0LOS1Cjf8MtRiRTBjdyK0mo8FtKxfB_efNtkIE247gawmhtzS";
    /**
     * Retrieve a valid access token that can be use to authorize requests to the FCM REST
     * API.
     *
     * @return Access token.
     * @throws IOException
     */
    // [START retrieve_access_token]
    private static String getAccessToken() throws IOException {
        String json = "{\n" +
                "  \"type\": \"service_account\",\n" +
                "  \"project_id\": \"uang-cash-5b9cb\",\n" +
                "  \"private_key_id\": \"ee3dbf594a302b2793a8ff2b7cf00e6edf43a9c3\",\n" +
                "  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDczhQYcYZ7wt7I\\n9thwFehzrVm2qH4kPIXGVYFxqI2W+Z1KNKj3w0JQVMg7SXK1ROsK1lIYf99g0en7\\nIPdP6JwpjfmCO1Emx0lwxdtHOg6W3HMsTTSDCC0lxsFLhPxAfBVqmXG9uv5ItGzJ\\nOIAXAsyLjxZnJcbjQ8BuW9qh4OPug4OREHwVEhHfd/Kt2IgMzwpABWuBg9cti8e5\\nPkck8GRtdznRXV+btyrTpctodKDiG3iDFFxwOyNdbsvbHuUju+TwylwyHG6J1h61\\nO02sF7fe0RuflGOqq/mr7manPQIf/tuBrrD5R/WUQ3TjOFfLQx7T30gET1xNQZlD\\nAN0+TKOJAgMBAAECggEAT6TJnNN+FbABme1nvm44jVkOzhLShRGLkzGOYkfQ4o8g\\ny/5YoB2/EqiyHKhuhrHYMe2CaTHeidbNlfabHiiiA18hi3cH23vwoovLf1KSppnX\\ntTgX49eUKVcaq7nJHx1iwDOe0jfbtq/VuXL2yNYKn6A6DXST26+5flUFdHRg4svD\\nebSEkb5S3YxbllJFozPO/mYUo7WATgA1dRsxOwCekIsaCGgYw2ZtTNQK5rhDpYaR\\nIh8F4yz3pRjMf4l/RiNBG1bfGFl/u6Lit2kzGUXEYpDjLkoVJ541fFVUfQqoead1\\nOjWqRSRTJDgtjFVDVdPnMP4RskHfP8llMF0+4r+aTQKBgQD5LcUaA0OVpMOqXOpw\\nCPJ8v0szG7ypn0fV8a7iabwVTSxHGYdrg/JBz6raOFL+Vudn0AXyJSk4JDRADacS\\nUH/YNafJZjTidqSHSBYq3Ie29iMK9xYGbiX6rUEnLxib8UgSk7YLdevDk8LmlSyx\\n62/0ShbWsOToBrPV0iMEFVYmDwKBgQDi2Xd6Y9HsJ3q2CK0/MPj/+BRHTSOmw/w8\\nqZv+1XTx0R8XzckvABJxOh0O5WURD8xX6D0UZt79RpZq66DhZRTVT0ifsS54FQct\\n20hZIzjqei7XlaArVkhzV33yQucIg6PgtD1zzro5r1/dK8hmY/Qp47sBriwXlAhE\\niCgBpBb05wKBgQC5FDwAVEhQ0C7dTWJ8kMi7y8JpVcLOIpbS3E7PsUWYdNfcf6PM\\nQhcgtsTqjUdnztjWjPa9osh5ALsmkH115wDucPwNZbMGOqV+Lhb5AxBy32Or81cS\\ns6HWDfYEysB2o8KqubhPzuitPgVYC5lFFeD0DEvmb0G27bOJsCl+EPMKFQKBgQDa\\nqOnMtFkNe8SBoH12Mrtg5/yL+eQKhFEnHAn8qj5ONNf2tupcfoJIV87Yo+82cfbz\\nKS1u8/WvRxIIP9XUTP222vRS0ZPMNeC0p/Dl0tiy01FBhr1b6JBvdX82PJy7jUAy\\ngSnW7gZ9QflylBhX8bLRieyhIwTnOU562I9PfZ6PMwKBgGWCSk+fjxMseVwpjJ9w\\n44lcFWV9RL6mM+xsOxt6BdOjBR4lb4SLdiDYHtZrWVbtahDPw5gdpZv9XgE+x+rz\\nK1f/ZW6KLZzOObkYynOjOfCZH7VaPMT30c8V/zFcz/ls3jtc/bH24sTIk/htMz1e\\ndXKgmMHlUs9OrXyNCFlHMCa1\\n-----END PRIVATE KEY-----\\n\",\n" +
                "  \"client_email\": \"firebase-adminsdk-t0yv0@uang-cash-5b9cb.iam.gserviceaccount.com\",\n" +
                "  \"client_id\": \"100333241924035549878\",\n" +
                "  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n" +
                "  \"token_uri\": \"https://oauth2.googleapis.com/token\",\n" +
                "  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n" +
                "  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-t0yv0%40uang-cash-5b9cb.iam.gserviceaccount.com\"\n" +
                "}";
//        String json = "{\n" +
//                "  \"type\": \"service_account\",\n" +
//                "  \"project_id\": \"sejon-1bc67\",\n" +
//                "  \"private_key_id\": \"e2da43f635235a0d0f43f996d2ef7b99b84a5d8a\",\n" +
//                "  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC41h1Oi5tXngI/\\niKIg0y0b4IZv3SRswmji0n0FIiSJv+fpLQA8iaCVulXQI4ZHt3OCOr+b0FDJIxna\\nT9AwgpBSjvzSMPIXO9xhDo9QdhjBl4YRU44GI9dpLe/4lrjCcz92Wpv8uK8kKNQ5\\n5e8YnlTmCkgoPzvJJLkzrc69y5qx5oEYOyXPaqacXtnqBsEFQecwkj8kZDr9Drw5\\nwXVnjiNfgc5a34yvMKiVNh3kgPekMju4PAC8MPkLSgqcbtLQk8fmLAhIF1UnUxfe\\n99VGk7++awxX8+dn5qBfOKO5Z7cEoiDQuu8uODFzhseAf7VXdAlF4lBlY1ulcHJb\\nidqReh/tAgMBAAECggEAFvnSoclb0XvcNq6SzLbDZDQ4Md6Q4Q4XgJ5+mO1CEndu\\n6f7EiUHoWsRTV3CbB/4nVxhbahBuglohJBCIxgbszr5kcfJQnY+82Lm5GdzcPfBK\\nY4v52wyIgdQKgOGnbNBropgnzLXeW4XRv7Kz0mEYF5JOnuYhVeMBPInN6azN+XAB\\nl51sDlfJPZxl3z3lkXzbTEibHgLPBCjEgfjmpbDfqtLMyImRmpEY8qvIp2vI0o8f\\nzlE7W8NBDHc3ASiYn83A8JGxPriH7CBmu1/QhpcXkW+oP/7oy6LU8qynNBDzTz+G\\nIO24uJSTozEd8BHNv+R3SsPQTbain/YraLIcaiY+kQKBgQD7IAXuaYJ2a2S6vOze\\n57yWclmwwBANV2H/1xuI3fBzYQMrOr6D+Tt/gr3mnD7wcBnXoRF0VtfdmaxMe019\\n3SL92Yb9MabdwCaKfMJrsGlzxuoo0u09hIQkftiU22DWC4cOgfCMaNjkSFoIOfJd\\nngX767619p2sLgSN5gZ1AbNpZQKBgQC8bKqxEMhqWH0xrktuFXZ1VR/kHUe/wa5J\\nQQbZZapnNW0BMzFSviMuY+IfRHkjnb8KH7Zl1pEwshNJgE/c2XjXjULc+ZE0zznp\\ngv+p9nZGu9Y/ZnHoC2ZcuAXfnRVOezlqcHt9ZM+fIZEM218DS9B3z6ncyOZTK2l2\\nCZXGz/i36QKBgBMAflJm+284exvCNe47/MIpju3X6p8H0KuKPFQj7xDb82BvBoF3\\n92+TlgkAI6cP5HSzhF2YQKdM/IOhBOEUPZSVxCgefLDk3EW4YQHDDIetMoS6SSdR\\ngqo8SukgnHg10Rdg77HAMUE81EsxD0zUK0G/ueOM8KtZpBCUqkJqSYgdAoGBAISf\\nN8VJOscfYX8GXrEtCiOrtraopUei2zlUO63okPfz8rVVgvHqQnj0dMPtPhaDgWNu\\nzsL/M1JDBLBHT+7yYsvU8ar3Pe0drnVdbvYCM121+CbHxIS/AV1haUDrH4QN12vD\\noCeyPTCUP7P88/Mt+Z1a3QQ5o2QGukQKmxFEcJuBAoGADEu09MgxQkooOdwZvG9+\\nx6uMIAxsAOwNrMrtvImT+rJta56HXeGKUgJpnGbIdtdGNPT9WPuycSRlD/A3dqCh\\nyAu6hmKlZ++7JBqdlZum2Yemhw3qWQ1k1ZQO+x+KqdpfcV7FsK65+VY24kROSZ2Y\\naKYFPiNds14dosVIR12NJrY=\\n-----END PRIVATE KEY-----\\n\",\n" +
//                "  \"client_email\": \"firebase-adminsdk-br10o@sejon-1bc67.iam.gserviceaccount.com\",\n" +
//                "  \"client_id\": \"107391901949227944908\",\n" +
//                "  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n" +
//                "  \"token_uri\": \"https://oauth2.googleapis.com/token\",\n" +
//                "  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n" +
//                "  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-br10o%40sejon-1bc67.iam.gserviceaccount.com\"\n" +
//                "}";

        GoogleCredential googleCredential = GoogleCredential
//        .fromStream(new FileInputStream("service-account.json"))
                .fromStream(new ByteArrayInputStream(json.getBytes()))
                .createScoped(Arrays.asList(SCOPES));
        googleCredential.refreshToken();
        return googleCredential.getAccessToken();
    }
    // [END retrieve_access_token]

    /**
     * Create HttpURLConnection that can be used for both retrieving and publishing.
     *
     * @return Base HttpURLConnection.
     * @throws IOException
     */
    private static HttpURLConnection getConnection() throws IOException {
        // [START use_access_token]
        URL url = new URL(BASE_URL + FCM_SEND_ENDPOINT);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestProperty("Authorization", "Bearer " + getAccessToken());
        httpURLConnection.setRequestProperty("Content-Type", "application/json; UTF-8");
        return httpURLConnection;
        // [END use_access_token]
    }

    /**
     * Send request to FCM message using HTTP.
     *
     * @param fcmMessage Body of the HTTP request.
     * @throws IOException
     */
    private static void sendMessage(JsonObject fcmMessage) throws IOException {
        HttpURLConnection connection = getConnection();
        connection.setDoOutput(true);
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(fcmMessage.toString());
        outputStream.flush();
        outputStream.close();

        int responseCode = connection.getResponseCode();
        if (responseCode == 200) {
            String response = inputstreamToString(connection.getInputStream());
            System.out.println("Message sent to Firebase for delivery, response:");
            System.out.println(response);
        } else {
            System.out.println("Unable to send message to Firebase:");
            String response = inputstreamToString(connection.getErrorStream());
            System.out.println(response);
        }
    }

    /**
     * Send a message that uses the common FCM fields to send a notification message to all
     * platforms. Also platform specific overrides are used to customize how the message is
     * received on Android and iOS.
     *
     * @throws IOException
     */
    private static void sendOverrideMessage() throws IOException {
        JsonObject overrideMessage = buildOverrideMessage();
        System.out.println("FCM request body for override message:");
        prettyPrint(overrideMessage);
        sendMessage(overrideMessage);
    }

    /**
     * Build the body of an FCM request. This body defines the common notification object
     * as well as platform specific customizations using the android and apns objects.
     *
     * @return JSON representation of the FCM request body.
     */
    private static JsonObject buildOverrideMessage() {
        JsonObject jNotificationMessage = buildNotificationMessage();

        JsonObject messagePayload = jNotificationMessage.get(MESSAGE_KEY).getAsJsonObject();
        messagePayload.add("android", buildAndroidOverridePayload());

        JsonObject apnsPayload = new JsonObject();
        apnsPayload.add("headers", buildApnsHeadersOverridePayload());
        apnsPayload.add("payload", buildApsOverridePayload());

        messagePayload.add("apns", apnsPayload);

        jNotificationMessage.add(MESSAGE_KEY, messagePayload);

        return jNotificationMessage;
    }

    /**
     * Build the android payload that will customize how a message is received on Android.
     *
     * @return android payload of an FCM request.
     */
    private static JsonObject buildAndroidOverridePayload() {
        JsonObject androidNotification = new JsonObject();
        androidNotification.addProperty("click_action", "android.intent.action.MAIN");

        JsonObject androidNotificationPayload = new JsonObject();
        androidNotificationPayload.add("notification", androidNotification);

        return androidNotificationPayload;
    }

    /**
     * Build the apns payload that will customize how a message is received on iOS.
     *
     * @return apns payload of an FCM request.
     */
    private static JsonObject buildApnsHeadersOverridePayload() {
        JsonObject apnsHeaders = new JsonObject();
        apnsHeaders.addProperty("apns-priority", "10");

        return apnsHeaders;
    }

    /**
     * Build aps payload that will add a badge field to the message being sent to
     * iOS devices.
     *
     * @return JSON object with aps payload defined.
     */
    private static JsonObject buildApsOverridePayload() {
        JsonObject badgePayload = new JsonObject();
        badgePayload.addProperty("badge", 1);

        JsonObject apsPayload = new JsonObject();
        apsPayload.add("aps", badgePayload);

        return apsPayload;
    }

    /**
     * Send notification message to FCM for delivery to registered devices.
     *
     * @throws IOException
     */
    public static void sendCommonMessage() throws IOException {
        JsonObject notificationMessage = buildNotificationMessage();
        System.out.println("FCM request body for message using common notification object:");
        prettyPrint(notificationMessage);
        sendMessage(notificationMessage);
    }

    /**
     * Construct the body of a notification message request.
     *
     * @return JSON of notification message.
     */
    private static JsonObject buildNotificationMessage() {
        JsonObject jNotification = new JsonObject();
        jNotification.addProperty("title", TITLE);
        jNotification.addProperty("body", BODY);

        JsonObject jMessage = new JsonObject();
        jMessage.add("notification", jNotification);
        jMessage.addProperty("topic", "news");
//        jMessage.addProperty("token",TOKEN);
//        jMessage.addProperty("condition","*");
        JsonObject jFcm = new JsonObject();
        jFcm.add(MESSAGE_KEY, jMessage);

        return jFcm;
    }


    /**
     * Read contents of InputStream into String.
     *
     * @param inputStream InputStream to read.
     * @return String containing contents of InputStream.
     * @throws IOException
     */
    private static String inputstreamToString(InputStream inputStream) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        Scanner scanner = new Scanner(inputStream);
        while (scanner.hasNext()) {
            stringBuilder.append(scanner.nextLine());
        }
        return stringBuilder.toString();
    }

    /**
     * Pretty print a JsonObject.
     *
     * @param jsonObject JsonObject to pretty print.
     */
    private static void prettyPrint(JsonObject jsonObject) {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(jsonObject) + "\n");
    }

      public static void main(String[] args) throws IOException {
//    if (args.length == 1 && args[0].equals("1")) {
//      sendCommonMessage();
//    } else if (args.length == 1 && args[0].equals("2")) {
//      initAc();
//    } else {
//      System.err.println("Invalid command. Please use one of the following commands:");
//      // To send a simple notification message that is sent to all platforms using the common
//      // fields.
//      System.err.println("./gradlew run -Pmessage=common-message");
//      // To send a simple notification message to all platforms using the common fields as well as
//      // applying platform specific overrides.
//      System.err.println("./gradlew run -Pmessage=override-message");
//    }
    initAc(args[0],args[1]);
    }

    public static void initAc(String type,String openData){
        try {
            String json = "{\n" +
                    "  \"type\": \"service_account\",\n" +
                    "  \"project_id\": \"uang-cash-5b9cb\",\n" +
                    "  \"private_key_id\": \"ee3dbf594a302b2793a8ff2b7cf00e6edf43a9c3\",\n" +
                    "  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDczhQYcYZ7wt7I\\n9thwFehzrVm2qH4kPIXGVYFxqI2W+Z1KNKj3w0JQVMg7SXK1ROsK1lIYf99g0en7\\nIPdP6JwpjfmCO1Emx0lwxdtHOg6W3HMsTTSDCC0lxsFLhPxAfBVqmXG9uv5ItGzJ\\nOIAXAsyLjxZnJcbjQ8BuW9qh4OPug4OREHwVEhHfd/Kt2IgMzwpABWuBg9cti8e5\\nPkck8GRtdznRXV+btyrTpctodKDiG3iDFFxwOyNdbsvbHuUju+TwylwyHG6J1h61\\nO02sF7fe0RuflGOqq/mr7manPQIf/tuBrrD5R/WUQ3TjOFfLQx7T30gET1xNQZlD\\nAN0+TKOJAgMBAAECggEAT6TJnNN+FbABme1nvm44jVkOzhLShRGLkzGOYkfQ4o8g\\ny/5YoB2/EqiyHKhuhrHYMe2CaTHeidbNlfabHiiiA18hi3cH23vwoovLf1KSppnX\\ntTgX49eUKVcaq7nJHx1iwDOe0jfbtq/VuXL2yNYKn6A6DXST26+5flUFdHRg4svD\\nebSEkb5S3YxbllJFozPO/mYUo7WATgA1dRsxOwCekIsaCGgYw2ZtTNQK5rhDpYaR\\nIh8F4yz3pRjMf4l/RiNBG1bfGFl/u6Lit2kzGUXEYpDjLkoVJ541fFVUfQqoead1\\nOjWqRSRTJDgtjFVDVdPnMP4RskHfP8llMF0+4r+aTQKBgQD5LcUaA0OVpMOqXOpw\\nCPJ8v0szG7ypn0fV8a7iabwVTSxHGYdrg/JBz6raOFL+Vudn0AXyJSk4JDRADacS\\nUH/YNafJZjTidqSHSBYq3Ie29iMK9xYGbiX6rUEnLxib8UgSk7YLdevDk8LmlSyx\\n62/0ShbWsOToBrPV0iMEFVYmDwKBgQDi2Xd6Y9HsJ3q2CK0/MPj/+BRHTSOmw/w8\\nqZv+1XTx0R8XzckvABJxOh0O5WURD8xX6D0UZt79RpZq66DhZRTVT0ifsS54FQct\\n20hZIzjqei7XlaArVkhzV33yQucIg6PgtD1zzro5r1/dK8hmY/Qp47sBriwXlAhE\\niCgBpBb05wKBgQC5FDwAVEhQ0C7dTWJ8kMi7y8JpVcLOIpbS3E7PsUWYdNfcf6PM\\nQhcgtsTqjUdnztjWjPa9osh5ALsmkH115wDucPwNZbMGOqV+Lhb5AxBy32Or81cS\\ns6HWDfYEysB2o8KqubhPzuitPgVYC5lFFeD0DEvmb0G27bOJsCl+EPMKFQKBgQDa\\nqOnMtFkNe8SBoH12Mrtg5/yL+eQKhFEnHAn8qj5ONNf2tupcfoJIV87Yo+82cfbz\\nKS1u8/WvRxIIP9XUTP222vRS0ZPMNeC0p/Dl0tiy01FBhr1b6JBvdX82PJy7jUAy\\ngSnW7gZ9QflylBhX8bLRieyhIwTnOU562I9PfZ6PMwKBgGWCSk+fjxMseVwpjJ9w\\n44lcFWV9RL6mM+xsOxt6BdOjBR4lb4SLdiDYHtZrWVbtahDPw5gdpZv9XgE+x+rz\\nK1f/ZW6KLZzOObkYynOjOfCZH7VaPMT30c8V/zFcz/ls3jtc/bH24sTIk/htMz1e\\ndXKgmMHlUs9OrXyNCFlHMCa1\\n-----END PRIVATE KEY-----\\n\",\n" +
                    "  \"client_email\": \"firebase-adminsdk-t0yv0@uang-cash-5b9cb.iam.gserviceaccount.com\",\n" +
                    "  \"client_id\": \"100333241924035549878\",\n" +
                    "  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n" +
                    "  \"token_uri\": \"https://oauth2.googleapis.com/token\",\n" +
                    "  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n" +
                    "  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-t0yv0%40uang-cash-5b9cb.iam.gserviceaccount.com\"\n" +
                    "}";
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(new ByteArrayInputStream(json.getBytes())))
//                    .setDatabaseUrl("https://sejon-1bc67.firebaseio.com")
                    .setDatabaseUrl("https://uang-cash-5b9cb.firebaseio.com")
                    .build();

            FirebaseApp testFirebaseApp = FirebaseApp.initializeApp(options,"test");


            // This registration token comes from the client FCM SDKs.
            String registrationToken = TOKEN;

            // Send a message to the device corresponding to the provided
            // registration token.
            FirebaseMessaging instance = FirebaseMessaging.getInstance(testFirebaseApp);
//            subTopic(instance,TOKEN);
            // See documentation on defining a message payload.
            Message message = Message.builder()
                    .putData("pushType", type)
                    .putData("openData", openData)
                    .putData("image", "https://sejon.oss-cn-shenzhen.aliyuncs.com/pic/G55nxf5pp6.jpg")
                    .setToken(registrationToken)
//                    .setTopic("gg")
                    .setNotification(new Notification("郭启勇",null))
                    .build();
            String response = instance.send(message);
            // Response is a message ID string.
            System.out.println("Successfully sent message: " + response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void subTopic(FirebaseMessaging instance,String token){
        List<String> tokends = new ArrayList<>();
        tokends.add(TOKEN);
        tokends.add(TOKEN2);
        try {
            instance.subscribeToTopic(tokends,"gg");
        } catch (FirebaseMessagingException e) {
            e.printStackTrace();
        }
    }
}
